package cn.b.sky.user.controller;

import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.utils.DateUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.service.LoginLogService;
import cn.b.sky.user.service.model.LoginLog;
import cn.b.sky.user.service.model.condition.LoginLogCondition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/log/*")
@Slf4j
public class LoginLogController extends SecurityController {
  @Resource
  LoginLogService loginLogService;

  @RequestMapping("onlineLogList")
  public String onlineLogList(Model model, @ModelAttribute("condition") LoginLogCondition log, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    log.setNow(new Date());
    log.setSiteCode(SecurityContextUtil.getSiteCode());
    List<LoginLog> logs = loginLogService.selectOnLineLog(log);
    int count = loginLogService.selectOnLineLogCount(log.getSiteCode());
    log.setData(logs);
    log.setTotalCount(count);
    model.addAttribute("page", log);
    return "log/online_log_list";
  }

  @RequestMapping("loginLogList")
  public String loginLog(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "log/login_log_list";
  }

  @RequestMapping("hisLogList")
  public String hisLogList(Model model, @ModelAttribute("condition") LoginLogCondition log, String navTabId)
      throws ParseException {
    // 时区转换
    if (!StringUtils.isEmpty(log.getFromDateTimeStr())) {
      log.setFromDateTime(DateUtils.getDateUtils().getDateFromStr(log.getFromDateTimeStr()));
    } else {
      log.setFromDateTime(null);
    }
    if (!StringUtils.isEmpty(log.getToDateTimeStr())) {
      log.setToDateTime(DateUtils.getDateUtils().getDateFromStr(log.getToDateTimeStr()));
    } else {
      log.setToDateTime(null);
    }
    model.addAttribute(NAVTABID, navTabId);
    log.setNow(new Date());
    log.setSiteCode(SecurityContextUtil.getSiteCode());
    List<LoginLog> logs = loginLogService.selectHisLog(log);
    int count = loginLogService.selectHisLogCount(log);
    log.setData(logs);
    log.setTotalCount(count);
    model.addAttribute("page", log);
    return "log/his_log_list";
  }

  @RequestMapping("deleteLog")
  @ResponseBody
  public DWZResult deleteLog(Model model, String rel, String navTabId, HttpServletRequest request) {
    try {
      String ids = request.getParameter("ids");
      String[] logs = ids.split(",");
      for (String log : logs) {
        loginLogService.deleteLog(Long.parseLong(log));
      }
      return navTabAjaxDone(null, rel, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);

    } catch (Exception e) {
      log.error("{}", e);
      model.addAttribute("message", e.getMessage());
      return ajaxError(getImeViewMessageI18n(null,
          "failedOperation.M"));
    }

  }

}
